package com.nit.tools.doc;

import cn.hutool.extra.template.TemplateException;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

public class GuoFtlUtil {

    /**
     * ftl文件替换数据并返回字符串
     *
     * @param ftlDir
     * @param ftlName
     * @param paramMap
     * @return
     * @throws IOException
     * @throws TemplateException
     */
    public static String ftlToString(String ftlDir, String ftlName, Object paramMap) throws IOException, TemplateException, freemarker.template.TemplateException {
        Configuration cfg = new Configuration(Configuration.getVersion());
        cfg.setDefaultEncoding("UTF-8");
//        cfg.setNumberFormat("0.00");
        cfg.setDirectoryForTemplateLoading(new File(ftlDir));
        Template template = cfg.getTemplate(ftlName);
        StringWriter writer = new StringWriter();
        template.process(paramMap, writer);
        writer.flush();
        String htmlTemplate = writer.toString();
        writer.close();
        return htmlTemplate;
    }
}
